package org.dromara.xhlj.crud.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import org.dromara.common.satoken.utils.LoginHelper;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.xhlj.domain.vo.XhljTopicVo;
import org.dromara.xhlj.domain.query.XhljTopicQuery;
import org.dromara.xhlj.domain.bo.XhljTopicBo;
import org.dromara.xhlj.crud.service.IXhljTopicCrudService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * xhlj-crud-话题
 *
 * @author yf
 * @date 2024-07-28
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/xhlj/topic")
public class XhljTopicCrudController extends BaseController {

    private final IXhljTopicCrudService xhljTopicService;

    /**
     * 分页查询话题列表
     */
    @SaCheckPermission("xhlj:topic:list")
    @GetMapping("/list")
    public TableDataInfo<XhljTopicVo> list(XhljTopicQuery query, PageQuery pageQuery) {
         return xhljTopicService.queryPageList(query, pageQuery);
    }

    /**
     * 查询话题列表
     */
    @SaCheckPermission("xhlj:topic:list")
    @GetMapping("/allList")
    public TableDataInfo<XhljTopicVo> allList(XhljTopicQuery query) {
        List<XhljTopicVo> list =  xhljTopicService.queryList(query);
        return TableDataInfo.build(list);
    }


    /**
     * 导出话题列表
     */
    @SaCheckPermission("xhlj:topic:export")
    @Log(title = "话题", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(XhljTopicQuery query, HttpServletResponse response) {
        List<XhljTopicVo> list = xhljTopicService.queryList(query);
        ExcelUtil.exportExcel(list, "话题", XhljTopicVo.class, response);
    }

    /**
     * 获取话题详细信息
     *
     * @param topicId 主键
     */
    @SaCheckPermission("xhlj:topic:query")
    @GetMapping("/{topicId}")
    public R<XhljTopicVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Integer topicId) {
        return R.ok(xhljTopicService.queryById(topicId));
    }

    /**
     * 新增话题
     */
    @SaCheckPermission("xhlj:topic:add")
    @Log(title = "话题", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody XhljTopicBo bo) {
        return toAjax(xhljTopicService.insertByBo(bo));
    }

    /**
     * 修改话题
     */
    @SaCheckPermission("xhlj:topic:edit")
    @Log(title = "话题", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody XhljTopicBo bo) {
        return toAjax(xhljTopicService.updateByBo(bo));
    }

    /**
     * 删除话题
     *
     * @param topicIds 主键串
     */
    @SaCheckPermission("xhlj:topic:remove")
    @Log(title = "话题", businessType = BusinessType.DELETE)
    @DeleteMapping("/{topicIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Integer[] topicIds) {
        return toAjax(xhljTopicService.deleteWithValidByIds(List.of(topicIds), true));
    }
}
